<template>
  <div style="margin:10px">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="true" label-width="85px">
      <el-form-item label="提现单号" prop="extractId">
        <el-input
          style="width: 140px"
          v-model.trim="queryParams.extractId"
          placeholder="请输入提现单号"
          clearable/>
      </el-form-item>
      <el-form-item label="用户ID" prop="userId">
        <el-input
          style="width: 140px"
          v-model.trim="queryParams.userId"
          placeholder="请输入用户ID"
          clearable/>
      </el-form-item>
      <el-form-item label="收款姓名" prop="alipayName">
        <el-input
          style="width: 140px"
          v-mode.triml="queryParams.alipayName"
          placeholder="请输入收款姓名"
          clearable/>
      </el-form-item>
      <el-form-item label="支付宝账号" prop="alipayAccount">
        <el-input
          style="width: 150px"
          v-mode.triml="queryParams.alipayAccount"
          placeholder="请输入支付宝账号"
          clearable/>
      </el-form-item>
      <el-form-item label="单据状态" prop="status">
        <el-select v-model="queryParams.status"
                   placeholder="请选择单据状态" clearable>
          <el-option
            v-for="(item,index) of payStatus"
            :label="item"
            :value="item">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="extractList">
      <el-table-column label="用户ID" align="center" prop="userId"/>
      <el-table-column label="提现单号" align="center" prop="extractId"/>
      <el-table-column label="收款姓名" align="center" prop="alipayName"/>
      <el-table-column label="支付宝账号" align="center" prop="alipayAccount"/>
      <el-table-column label="提现金额" align="center" prop="amount"/>
      <el-table-column label="转账订单号" align="center" prop="orderId"/>
      <el-table-column label="单据状态" align="center" width="110">
        <template slot-scope="scope">
          {{ scope.row.status }}
          <button v-if="disStatus.includes(scope.row.status)" @click="cashOut(scope.row.extractId)">转账</button>
        </template>
      </el-table-column>
      <el-table-column label="支付时间" align="center" prop="transDate"/>
      <el-table-column label="资金流水号" align="center" prop="payFundOrderId"/>
      <el-table-column label="更新时间" align="center" prop="updateTime"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-refresh"
            @click="flash(scope.row)"
            v-hasPermi="['system:extract:remove']">刷新转账结果
          </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:extract:edit']">修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:extract:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"/>
    <!-- 添加或修改提现对话框 -->
    <el-dialog title="修改提现" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="收款方真实姓名" prop="alipayName">
          <el-input v-model.trim="form.alipayName" placeholder="请输入收款方真实姓名"/>
        </el-form-item>
        <el-form-item label="支付宝账号" prop="alipayAccount">
          <el-input v-model.trim="form.alipayAccount" placeholder="请输入支付宝账号"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {listExtract, getExtract, delExtract, updateExtract, extractFlash, admCashOut} from "@/api/system/extract";

  export default {
    name: "Extract",
    data() {
      return {
        disStatus: ['未转账', '审批中', '失败'],
        payStatus: ['未转账', '审批中', '转账中', '已转账', '失败'],
        // 遮罩层
        loading: true,

        // 总条数
        total: 0,
        // 提现表格数据
        extractList: [],
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,

          extractId: null,
          userId: null,
          alipayName: null,
          alipayAccount: null,
          status: null
        },
        // 表单参数
        form: {
          alipayName: null,
          alipayAccount: null
        },
        // 表单校验
        rules: {
          alipayName: [
            {required: true, message: "收款方真实姓名不能为空", trigger: "blur"}
          ],
          alipayAccount: [
            {required: true, message: "支付宝账号不能为空", trigger: "blur"}
          ]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      flash(item) {
        this.loading = true;
        extractFlash(item).then(res => {
          if (res.code == 200) {
            this.$modal.msgSuccess(res.msg);
          } else {
            this.$modal.msgWarning(res.msg);
          }
        }).catch(() => {
          this.loading = false
        }).finally(() => {
          this.getList()
        })
      },
      cashOut(id) {
        this.loading = true;
        admCashOut(id).then(res => {
          if (res.code == 200) {
            this.$modal.msgSuccess(res.msg);
          } else {
            this.$modal.msgWarning(res.msg);
          }
        }).catch(() => {
          this.loading = false
        }).finally(() => {
          this.getList()
        })
      },
      /** 查询提现列表 */
      getList() {
        this.loading = true;
        listExtract(this.queryParams).then(response => {
          this.extractList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          extractId: null,
          alipayName: null,
          alipayAccount: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        getExtract(row.extractId).then(response => {
          this.form.extractId = response.data.extractId;
          this.form.alipayName = response.data.alipayName;
          this.form.alipayAccount = response.data.alipayAccount;
          this.open = true;
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            updateExtract(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            })
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$modal.confirm('是否确认删除提现编号为"' + row.extractId + '"的数据项？').then(function () {
          return delExtract(row.extractId);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      }
    }
  };
</script>
